#include "q_shared.h"
#include "bg_gamex86funcs.h"
#include "bg_global.h"
#include "g_local.h"

float waypoint_getRadius(gentity_t *ent)
{
	float minDist = 1025.0f;
	float dist;
	int i;

	for(i = 0; i < 16; i++)
	{
		dist = waypoint_testDirection(ent->currentOrigin, ((360.0f/16)*i), minDist);

		if(dist < minDist)
		{
			minDist = dist;
		}
	}

	return minDist + 16;
}

qboolean SP_Waypoint ( gentity_t *ent )
{
	if(!ent->radius)
	{
		//Long story short: The radius checks don't work properly. Use the old system.
		return qfalse;
	}

	VectorSet(&ent->mins, -16, -16, DEFAULT_MINS_2);
	VectorSet(&ent->maxs, 16, 16, DEFAULT_MAXS_2);

	ent->contents = CONTENTS_TRIGGER;
	ent->clipmask = MASK_DEADSOLID;

	gi->linkentity( ent );

	ent->count = -1;
	ent->classname = "waypoint";

	if(ent->spawnflags&2)
	{
		ent->currentOrigin[2] += 128.0f; //Not very subtle. But it seems to have gotten the job done.
	}
	if( !(ent->spawnflags&1) && G_CheckInSolid (ent, qtrue))
	{
		//if not SOLID_OK, and in solid
		ent->maxs[2] = CROUCH_MAXS_2;

		if(G_CheckInSolid (ent, qtrue))
		{
			gi->Printf("^1ERROR: Waypoint '%s' in solid (@%s)\n", ent->targetname, vtos(ent->currentOrigin));
			delayedShutdown = level_time + 100;
			G_FreeEntity(ent);
			return qtrue;
		}
	}
	NAV__SpawnedPoint(ent, PT_WAYNODE);
	G_FreeEntity(ent);
	return qtrue;
}

// Redoing the spawn function because in JA, they broke this very very badly.
void SP_NPC_Spawner2 ( gentity_t *ent )
{
	if(ent->spawnflags & 256 && g_spskill->integer == 0)
	{
		//Not in Padawan
		return;
	}
	if(ent->spawnflags & 512 && g_spskill->integer == 1)
	{
		//Not in Jedi
		return;
	}
	if(ent->spawnflags & 1024 && g_spskill->integer == 2)
	{
		//Not in Jedi Knight
		return;
	}
	if(ent->spawnflags & 2048 && g_spskill->integer == 3)
	{
		//Not in Jedi Master
		return;
	}
	SP_NPC_Spawner(ent);
}

// Let's blow this joint. JK2 style.
void SP_waypoint_navgoal_new ( gentity_t *ent )
{
	float radius = (ent->radius) ? (ent->radius) : 12;

	if(!Q_stricmp(ent->classname, "waypoint_navgoal_8"))
	{
		VectorSet(&ent->mins, -8, -8, -2);
		VectorSet(&ent->maxs, 8, 8, 16);
	}
	else if(!Q_stricmp(ent->classname, "waypoint_navgoal_4"))
	{
		VectorSet(&ent->mins, -4, -4, -1);
		VectorSet(&ent->maxs, 4, 4, 8);
	}
	else if(!Q_stricmp(ent->classname, "waypoint_navgoal_2"))
	{
		VectorSet(&ent->mins, -2, -2, -1);
		VectorSet(&ent->maxs, 2, 2, 4);
	}
	else if(!Q_stricmp(ent->classname, "waypoint_navgoal_1"))
	{
		VectorSet(&ent->mins, -1, -1, -1);
		VectorSet(&ent->maxs, 1, 1, 2);
	}
	else
	{
		VectorSet(&ent->mins, -16, -16, -24);
		VectorSet(&ent->maxs, 16, 16, 32);
	}
	ent->origin[2] += 0.125f;
	if(!(ent->spawnflags&1) && G_CheckInSolid(ent, qfalse))
	{
		gi->Printf("^1ERROR: Waypoint_navgoal %s at %s in solid!\n", ent->targetname, vtos(ent->currentOrigin));
		return;
	}
	TAG_Add( ent->targetname, NULL, ent->origin, ent->angles, radius, RTF_NAVGOAL );

	ent->classname = "navgoal";
	G_FreeEntity( ent );
}

//--Does not exist in JKA (exists in JK2 however)--
void SP_waypoint_navgoal_4 ( gentity_t *ent )
{
	gi->Printf("^2waypoint_navgoal_4 loaded ^.^\n");
	SP_waypoint_navgoal_new(ent);
	VectorSet2(&ent->mins, -4);
	VectorSet2(&ent->maxs, 4);
}

void SP_waypoint_navgoal_8 ( gentity_t *ent )
{
	gi->Printf("^2waypoint_navgoal_8 loaded ^.^\n");
	VectorSet2(&ent->mins, -8);
	VectorSet2(&ent->maxs, 8);
	SP_waypoint_navgoal_new(ent);
}

void SP_waypoint_navgoal_2 ( gentity_t *ent )
{
	gi->Printf("^2waypoint_navgoal_2 loaded ^.^\n");
	VectorSet2(&ent->mins, -2);
	VectorSet2(&ent->maxs, 2);
	SP_waypoint_navgoal_new(ent);
}

void SP_waypoint_navgoal_1 ( gentity_t *ent )
{
	gi->Printf("^2waypoint_navgoal_1 loaded ^.^\n");
	VectorSet2(&ent->mins, -1);
	VectorSet2(&ent->maxs, 1);
	SP_waypoint_navgoal_new(ent);
}

//Exists in JKA, but is broken
void SP_NPC_Galak ( gentity_t *ent )
{
	if(ent->spawnflags & 1)
	{
		//Mech armor
		ent->NPC_type = "Galak_Mech";
	}
	else
	{
		ent->NPC_type = "Galak";
	}
	SP_NPC_Spawner2(ent);
}